'''
selenium的基础操作进行封装
'''
import time

from selenium import webdriver
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.select import Select
from selenium.webdriver.support.wait import WebDriverWait


# 定义一个函数，来获取不同浏览器的驱动
def getDriver(driverStr):
    if driverStr=="safari":
        driver = webdriver.Safari()
    elif driverStr=="chrome":
        driver = webdriver.Chrome()
    elif driverStr=="firefox":
        driver = webdriver.Firefox()
    else:
        driver = webdriver.Edge()

    return driver

# 定义一个类，封装了不同的操作：关闭浏览器，定位元素 选择下拉框...
class BasePage:
    def __init__(self,driver:webdriver):
        self.driver = driver

    # 打开某个网页
    def open(self,url):
        self.driver.get(url)
        # 窗口最大化
        self.driver.maximize_window()
        # 隐式等待
        self.driver.implicitly_wait(5)

    # 退出浏览器
    def quit(self):
        self.driver.quit()

    # 关闭当前网页
    def close(self):
        self.driver.close()

    # 定位元素
    '''
    target的字符串格式：
    id=值
    class name = 值
    xpath = 值
    link text=值 
    '''
    def locate_element(self,target):
        try:
            # 处理target字符串
            list1 = target.split("=")
            locator = (list1[0].strip(" "),list1[1].strip(" "))

            # 显式等待
            element = WebDriverWait(self.driver, 5, 0.5).until(
                expected_conditions.presence_of_element_located(locator))

            print(element.get_dom_attribute("id"))
            return  element
        except Exception as e:
            print("定位失败：",e)

        # 发送信息
    def send(self, target, information):
        element = self.locate_element(target)
        element.clear()  # 先清空
        element.send_keys(information)

        # 点击元素
    def click(self,target):
        self.locate_element(target).click()

        # 跳转到iframe
    def switch_to_iframe(self, target):
        iframe_element = self.locate_element(target)
        self.driver.switch_to.frame(iframe_element)

        # 返回上一层
    def switch_to_parent_frame(self):
         self.driver.switch_to.parent_frame()

        # 返回到最外层
    def switch_to_default_content(self):
        self.driver.switch_to.default_content()

        # 根据下标进行选择下拉框
    def select_dropdown_by_index(self, target, index):
        element = self.locate_element(target)
        select = Select(element)
        select.select_by_index(index)

        # 根据文本进行选择下拉框

    def select_dropdown_by_visible_text(self, target, text):
        element = self.locate_element(target)
        select = Select(element)
        select.select_by_visible_text(text)

        # 根据value进行选择下拉框

    def select_dropdown_by_value(self, target, value):
        element = self.locate_element(target)
        select = Select(element)
        select.select_by_value(value)

if __name__ == '__main__':
    # 创建基础类对象
    bp = BasePage(getDriver("edge"))
    bp.open("http://127.0.0.1/ranzhi/www/")
    # 定位搜索框并输入内容
    bp.send("id=account", "admin")
    bp.send("id=password", "123456")
    # 定位登录按钮并点击
    bp.click("id=submit")
    bp.click("id=s-menu-superadmin")
    #切换iframe页面
    bp.switch_to_iframe("id=iframe-superadmin")
    #点击添加成员
    bp.click("class name=shortcut.user")
    #点击角色下拉框
    bp.select_dropdown_by_index("id=role",3)
    bp.select_dropdown_by_visible_text("id=dept","/开发")

    # 等待搜索结果加载
    time.sleep(3)
    # 关闭浏览器
    bp.quit()